package org.zju.demo.shoppingdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.model.ContentModel;

import java.util.List;

/**
 * Created by ZJDX on 2015/9/24.
 */
public class DrawerListAdapter  extends BaseAdapter{
    //要更改listview的字体大和颜色，要自定义一个识配器
        private Context context;
        private List<ContentModel> list;

        public DrawerListAdapter(Context context, List<ContentModel> list) {
            super();
            this.context = context;
            this.list = list;
        }
        @Override
        public Object getItem(int arg0) {
            return arg0;
        }
        @Override
        public int getCount()
        {
            return list.size();
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // position就是位置从0开始，convertView是Spinner,ListView中每一项要显示的view
            // 通常return 的view也就是convertView
            // parent就是父窗体了，也就是Spinner,ListView,GridView了.
           /* TextView mTextView = new TextView(getApplicationContext());
            mTextView.setText(itemTitle[position]);
            mTextView.setTextSize(15);
            mTextView.setTextColor(Color.WHITE);//设置字体颜色
            return mTextView;
*/
            ViewHold hold;
            if (convertView == null) {
                hold = new ViewHold();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.content_item, null);
                convertView.setTag(hold);
            }else {
                hold=(ViewHold) convertView.getTag();
            }
            //获得组件，实例化组件
            hold.imageView=(ImageView) convertView.findViewById(R.id.item_imageview);
            hold.textView=(TextView) convertView.findViewById(R.id.item_textview);
            //绑定数据
            hold.imageView.setImageResource(list.get(position).getImageView());
            hold.textView.setText(list.get(position).getText());
            return convertView;
        }

        class ViewHold {
            public ImageView imageView;
            public TextView textView;
        }
    }